package com.pkdl;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.atomic.AtomicReference;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

import com.pkdl.dao.NguoiDungDAO;
import com.pkdl.helpers.CommonVars;

@SuppressWarnings("serial")
public class DlgLogin extends JDialog {
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			DlgLogin dialog = new DlgLogin(null, true);
			dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private JTextField txtMaNguoiDung;
	private JButton btnDangNhap;
	private NguoiDungDAO ndDAO;
	private JPasswordField txtMatKhau;

	/**
	 * Create the dialog.
	 */
	public DlgLogin(Frame parent, boolean modal) {
		super(parent, modal);
		this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		this.setResizable(false);
		this.setTitle("Danh nhap");
		this.setBounds(100, 100, 376, 193);
		this.setLocationRelativeTo(null);
		{
			JPanel panel = new JPanel();
			panel.setBackground(SystemColor.textHighlight);
			this.getContentPane().add(panel, BorderLayout.NORTH);
			panel.setLayout(new BorderLayout(0, 0));

			JLabel lblNewLabel = new JLabel("\u0110\u0102NG NH\u1EACP");
			lblNewLabel.setPreferredSize(new Dimension(31, 32));
			lblNewLabel.setForeground(Color.WHITE);
			lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
			lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 18));
			panel.add(lblNewLabel, BorderLayout.NORTH);
		}
		{
			JPanel panel = new JPanel();
			this.getContentPane().add(panel, BorderLayout.CENTER);
			panel.setLayout(null);

			JLabel lblNewLabel_1 = new JLabel(
					"M\u00E3 ng\u01B0\u1EDDi d\u00F9ng");
			lblNewLabel_1.setBounds(24, 25, 93, 14);
			panel.add(lblNewLabel_1);

			this.txtMaNguoiDung = new JTextField();
			this.txtMaNguoiDung.setText("qtv001");
			this.txtMaNguoiDung.setBounds(114, 22, 234, 20);
			panel.add(this.txtMaNguoiDung);
			this.txtMaNguoiDung.setColumns(10);

			JLabel lblMtKhu = new JLabel("M\u1EADt kh\u1EA9u");
			lblMtKhu.setBounds(24, 53, 93, 14);
			panel.add(lblMtKhu);

			this.btnDangNhap = new JButton("\u0110\u0103ng nh\u1EADp");
			this.btnDangNhap.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					DlgLogin.this.dispose();
				}
			});
			this.btnDangNhap.setIcon(new ImageIcon(DlgLogin.class
					.getResource("/com/pkdl/resources/Access key.png")));
			this.btnDangNhap.setBounds(118, 82, 127, 23);
			panel.add(this.btnDangNhap);

			this.txtMatKhau = new JPasswordField();
			this.txtMatKhau.setText("123");
			this.txtMatKhau.setBounds(114, 50, 234, 20);
			panel.add(this.txtMatKhau);

			JButton btnThoat = new JButton("Tho\u00E1t");
			btnThoat.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					System.exit(0);
				}
			});
			btnThoat.setIcon(new ImageIcon(DlgLogin.class
					.getResource("/com/pkdl/resources/logout.png")));
			btnThoat.setBounds(255, 82, 93, 23);
			panel.add(btnThoat);
		}
	}

	/**
	 * Kiem tra tai khoan dang nhap
	 * 
	 * @param errText
	 * @return
	 */
	public boolean validateLogin(AtomicReference<Object> errText) {
		this.ndDAO = new NguoiDungDAO();
		CommonVars.g_oNguoiDung = this.ndDAO.selectById(this.txtMaNguoiDung
				.getText());
		if (CommonVars.g_oNguoiDung == null) {
			errText.set("Nguoi dung nay khong ton tai trong he thong");
			return false;
		} else {
			this.ndDAO = new NguoiDungDAO();
			CommonVars.g_oNguoiDung = this.ndDAO.selectByIdAndPassword(
					this.txtMaNguoiDung.getText(), this.txtMatKhau.getText());
			if (CommonVars.g_oNguoiDung == null) {
				errText.set("Ban da nhap sai mat khau");
				return false;
			}
			return true;
		}
	}
}
